package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.itheima.common.TokenHolder;
import com.itheima.entity.dto.setmeal.SetmealAddDto;
import com.itheima.entity.dto.setmeal.SetmealLAddDto;
import com.itheima.entity.dto.setmeal.SetmealUpDto;

import com.itheima.entity.pojo.Setmeal;
import com.itheima.entity.pojo.SetmealDishes;
import com.itheima.entity.vo.dish.DishByIdVo;
import com.itheima.entity.vo.setmeal.*;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.FlavorMapper;
import com.itheima.mapper.SetmealDishesMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-11
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishesMapper setmealDishesMapper;


    @Autowired
    DishMapper dishMapper;

    @Autowired
    FlavorMapper flavorMapper;



    /**
     * 新增套餐
     *
     * @param setmealAddDto
     * @return
     */
    @Override
    public Boolean addSetmeal(SetmealAddDto setmealAddDto) {
        //1.存入套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealAddDto, setmeal);

        //1.1获得一个id
        String setmealId = new Snowflake().nextIdStr();
        setmeal.setId(setmealId);

        //1.2存入操作人的id
        String currentId = TokenHolder.getCurrentId();
        setmeal.setCreateUser(currentId);
        setmeal.setUpdateUser(currentId);

        //1.3存入添加时间和操作时间
        setmeal.setCreateTime(new Date());
        setmeal.setUpdateTime(new Date());

        //1.4存入数据库
        setmealMapper.insert(setmeal);

        //2.存入套餐菜品联系表
        //2.1遍历集合
        List<SetmealLAddDto> setmealDishesDtos = setmealAddDto.getSetmealDishes();
        if(CollUtil.isEmpty(setmealDishesDtos)){
            return true;
        }

        for (SetmealLAddDto setmealDishDto : setmealDishesDtos) {

            //2.2判空
            if(Objects.isNull(setmealDishDto)){
                continue;
            }

            //2.3将dto对象复制给pojo对象
            SetmealDishes setmealDishes = new SetmealDishes();
            BeanUtils.copyProperties(setmealDishDto, setmealDishes);

            setmealDishes.setSetmealId(setmealId);
            //2.4获取id
            String id = new Snowflake().nextIdStr();
            setmealDishes.setId(id);

            //2.5添加时间和操作时间
            setmealDishes.setCreateTime(new Date());
            setmealDishes.setUpdateTime(new Date());

            //2.6存入数据库
            setmealDishesMapper.insert(setmealDishes);

        }

        return true;
    }

    /**
     * 分页获取套餐列表
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public SetmealPageVo getSetmealPage(String name, Integer page, Integer pageSize) {
        SetmealPageVo setmealPageVo = new SetmealPageVo();
        setmealPageVo.setCurrent(page);
        setmealPageVo.setSize(pageSize);


        if(StrUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }

        Integer count = setmealMapper.selectCountByName(name);

        if(count==0){
            return null;
        }

        setmealPageVo.setTotal(count);


        page=(page-1)*pageSize;


        List<SetmealPageLVo> setmealPageLVo = setmealMapper.selectPage(name, page, pageSize);

        setmealPageVo.setRecords(setmealPageLVo);



        return setmealPageVo;
    }

    /**
     * 根据id获取套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealByIdVo getSetmealById(String id) {
        //1.获取套餐表的信息
        //1.1根据id获取数据库

        SetmealByIdVo setmealByIdVo = setmealMapper.selectById(id);

        if(Objects.isNull(setmealByIdVo)){
            return setmealByIdVo;
        }

        //2.根据套餐id获取菜品表
        List<SetmealDishes> list = setmealDishesMapper.selectBySetmealId(id);

        setmealByIdVo.setSetmealDishes(list);


        return setmealByIdVo;
    }

    /**
     * 更新套餐信息
     *
     * @param setmealUpDto
     * @return
     */
    @Override
    public Boolean upSetmeal(SetmealUpDto setmealUpDto) {
        //1.存入套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealUpDto, setmeal);

        //1.1获得一个id
        String setmealId = setmealUpDto.getId();

        //1.2存入操作人的id
        String currentId = TokenHolder.getCurrentId();
        setmeal.setUpdateUser(currentId);

        //1.3存入添加时间和操作时间
        setmeal.setUpdateTime(new Date());

        //1.4存入数据库
        Boolean update = setmealMapper.update(setmeal);
        if(!update){
            return false;
        }

        //2.存入套餐菜品联系表


        setmealDishesMapper.deleted(setmealId);


        //2.1遍历集合
        List<SetmealLAddDto> setmealDishesDtos = setmealUpDto.getSetmealDishes();
        if(CollUtil.isEmpty(setmealDishesDtos)){
            return true;
        }

        for (SetmealLAddDto setmealDishDto : setmealDishesDtos) {

            //2.2判空
            if(Objects.isNull(setmealDishDto)){
                continue;
            }

            //2.3将dto对象复制给pojo对象
            SetmealDishes setmealDishes = new SetmealDishes();
            BeanUtils.copyProperties(setmealDishDto, setmealDishes);

            setmealDishes.setSetmealId(setmealId);
            //2.4获取id
            String id = new Snowflake().nextIdStr();
            setmealDishes.setId(id);

            //2.5添加时间和操作时间
            setmealDishes.setCreateTime(new Date());
            setmealDishes.setUpdateTime(new Date());

            //2.6存入数据库
            setmealDishesMapper.insert(setmealDishes);

        }

        return true;
    }

    /**
     * 批量删除
     *
     * @param idArry
     **/
    @Override
    public Boolean delSetmeal(String[] idArry) {
        //1.根据id删除套餐表的信息
        setmealMapper.deleteById(idArry);

        //2.根据id删除菜品联系表的信息

        setmealDishesMapper.deletedByidArry(idArry);

        return true;
    }

    /**
     * 批量启用或禁用
     *
     * @param status
     * @param idArry
     * @return
     */
    @Override
    public Boolean upSetmealStatus(Integer status, String[] idArry) {

        //1.获取操作用户

        //1.2存入操作人的id
        String currentId = TokenHolder.getCurrentId();

        //2.获取操作时间
        Date date = new Date();

        //3.修改信息
        setmealMapper.updateStatusArray(status,idArry,currentId,date);

        return true;
    }

    /**
     * 根据分类id和状态获取套餐列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<SetmealListVo> getSetmealList(String categoryId, String status) {
        List<SetmealListVo> setmealListVos = new ArrayList<>();

        //1.查询分类列表
        List<Setmeal> setmeals=   setmealMapper.selectList(categoryId,status);

        for (Setmeal setmeal : setmeals) {
            if(Objects.isNull(setmeal)){
                continue;
            }


            SetmealListVo setmealListVo = new SetmealListVo();
            BeanUtils.copyProperties(setmeal, setmealListVo);
            setmealListVos.add(setmealListVo);

        }


        return setmealListVos;
    }

    /**
     * 获取菜品详情
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDishVo getSetmealDish(String id) {

        DishByIdVo dishByIdVo = dishMapper.selectById(id);
        SetmealDishVo setmealDishVo = new SetmealDishVo();
        if(Objects.isNull(dishByIdVo)){

            return setmealDishVo;
        }
        BeanUtils.copyProperties(dishByIdVo, setmealDishVo);


        List<SetmealDishLVo> flavors = flavorMapper.selectByDishIdList(id);

        setmealDishVo.setFlavors(flavors);




        return setmealDishVo;
    }


}
